/**
 * Created by 83617 on 2018/12/31.
 */
var express = require('express')
var path = require('path')
var mongoose = require('mongoose')

//middleware
var cookieParser = require('cookie-parser')
var session = require('express-session')
var mongoStore = require('connect-mongo')(session)
var logger = require('morgan');
var favicon = require('express-favicon');
var multipart = require('connect-multiparty');



var port = process.env.PORT || 3000
var app = express()

//body-parser处理表单数据,将其封装成一个对象
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));

mongoose.connect('mongodb://localhost:27017/Society', {useNewUrlParser: true});

app.set('views','./app/views/pages')
app.set('view engine','jade')
app.set('trust proxy', 1)

//favorite icon
app.use(favicon(__dirname + '/bower_components/resorce/back.jpg'));

app.use(express.static(path.join(__dirname,'bower_components')))

app.use(cookieParser())
app.use(multipart());

app.use(session({
    secret:'society',
    store:new mongoStore({
        url:'mongodb://localhost:27017/Society',
        collection:'sessions'
    })
}))

if('development' === app.get('env')){
    app.set('showStackError',true)
    app.use(logger(':method :url :status'))
    app.locals.pretty = true
    mongoose.set('debug',true)
}

require('./config/routes')(app)
app.locals.moment = require('moment')
app.listen(port)
console.log('The server started on ' + port)

